#include "U2GLESSupport.h"
#include "U2LogManager.h"
#include "U2Exception.h"


U2EG_NAMESPACE_USING


void U2GLESSupport::setConfigOption(const U2String &name, const U2String &value)
{
    U2ConfigOptionMap::iterator it = mOptions.find(name);

    if (it == mOptions.end())
    {
        U2_EXCEPT(U2Exception::ERR_INVALIDPARAMS,
                    "Option named " + name +  " does not exist.",
                    "GLESSupport::setConfigOption");
    }
    else
    {
        it->second.currentValue = value;
    }
}

U2ConfigOptionMap& U2GLESSupport::getConfigOptions(void)
{
    return mOptions;
}

void U2GLESSupport::initialiseExtensions(void)
{
    // Set version string
    const GLubyte* pcVer = glGetString(GL_VERSION);

    assert(pcVer && "Problems getting GL version string using glGetString");

    U2String tmpStr = (const char*)pcVer;
    U2LogManager::getSingleton().logMessage("GL_VERSION = " + tmpStr);
    mVersion = tmpStr.substr(0, tmpStr.find(" "));

    // Get vendor
    const GLubyte* pcVendor = glGetString(GL_VENDOR);
    tmpStr = (const char*)pcVendor;
    U2LogManager::getSingleton().logMessage("GL_VENDOR = " + tmpStr);
    mVendor = tmpStr.substr(0, tmpStr.find(" "));

    // Get renderer
    const GLubyte* pcRenderer = glGetString(GL_RENDERER);
    tmpStr = (const char*)pcRenderer;
    U2LogManager::getSingleton().logMessage("GL_RENDERER = " + tmpStr);

    // Set extension list
    std::stringstream ext;
    U2String str;

    const GLubyte* pcExt = glGetString(GL_EXTENSIONS);
    U2LogManager::getSingleton().logMessage("GL_EXTENSIONS = " + U2String((const char*)pcExt));

    assert(pcExt && "Problems getting GL extension string using glGetString");

    ext << pcExt;

    while (ext >> str)
    {
		U2LogManager::getSingleton().logMessage("EXT:" + str);
        extensionList.insert(str);
    }
}

bool U2GLESSupport::checkExtension(const U2String& ext) const
{
    if(extensionList.find(ext) == extensionList.end())
        return false;

    return true;
}
